package controllers

import (
	"encoding/json"
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
	"time"
	"zy15_news/cache"
	"zy15_news/models"
)

type NewsController struct {
	beego.Controller
}

func (c *NewsController) Create() {
	var err error

	title := c.GetString("title")
	released := c.GetString("released")
	types := c.GetString("types")

	if title == "" || released == "" || types == "" {
		c.Data["json"] = map[string]interface{}{
			"code":    500,
			"message": "数据不能为空",
		}
		c.ServeJSON()
		return
	}

	news := models.News{
		Title:    title,
		Released: released,
		Types:    types,
	}

	if err = news.Create(); err != nil {
		c.Data["json"] = map[string]interface{}{
			"code":    500,
			"message": "系统错误",
		}
		c.ServeJSON()
		return
	}

	key := fmt.Sprintf("news_newsid_%d", news.ID)
	jsonData, _ := json.Marshal(news)
	cache.Rdb.Set(cache.Ctx, key, jsonData, time.Hour)

	c.Data["json"] = map[string]interface{}{
		"code":    200,
		"message": "添加成功",
	}
	c.ServeJSON()
}
func (c *NewsController) List() {
	var err error
	page, _ := c.GetInt("page")
	pageSize, _ := c.GetInt("pageSize")

	key := fmt.Sprintf("news_list_%d", page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, key).Result()

	var news models.News
	var list []models.News

	if cacheData == "" {
		list, _ := news.List(page, pageSize)
		if len(list) != 0 {
			jsonData, _ := json.Marshal(list)
			cache.Rdb.Set(cache.Ctx, key, jsonData, time.Hour)
		}
	} else {
		fmt.Println("已从缓存中获取")
		json.Unmarshal([]byte(cacheData), &list)
	}

	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"code":    500,
			"message": "查询失败",
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = map[string]interface{}{
		"code":    500,
		"message": "查询成功",
		"data":    list,
	}
	c.ServeJSON()
}
func (c *NewsController) Update() {
	var err error

	id, _ := c.GetInt("id")
	title := c.GetString("title")
	released := c.GetString("released")
	types := c.GetString("types")

	if title == "" || released == "" || types == "" {
		c.Data["json"] = map[string]interface{}{
			"code":    500,
			"message": "数据不能为空",
		}
		c.ServeJSON()
		return
	}

	var news models.News
	news.ID = uint(id)
	news.Title = title
	news.Released = released
	news.Types = types

	if err = news.GetNewsById(); err != nil {
		c.Data["json"] = map[string]interface{}{
			"code":    500,
			"message": "ID获取错误",
		}
		c.ServeJSON()
		return
	}

	if err = news.Update(); err != nil {
		c.Data["json"] = map[string]interface{}{
			"code":    500,
			"message": "修改失败",
		}
		c.ServeJSON()
		return
	}

	key := fmt.Sprintf("news_updateId_%d", news.ID)
	jasonData, _ := json.Marshal(news)
	cache.Rdb.Set(cache.Ctx, key, jasonData, time.Hour)

	c.Data["json"] = map[string]interface{}{
		"code":    200,
		"message": "修改成功",
	}
	c.ServeJSON()
}
func (c *NewsController) Delete() {
	var err error
	id, _ := c.GetInt("id")
	var news models.News
	news.ID = uint(id)

	if err = news.GetNewsById(); err != nil {
		c.Data["json"] = map[string]interface{}{
			"code":    500,
			"message": "系统异常",
		}
		c.ServeJSON()
		return
	}

	if err = news.Del(); err != nil {
		c.Data["json"] = map[string]interface{}{
			"code":    500,
			"message": "删除失败",
		}
		c.ServeJSON()
		return
	}

	key := fmt.Sprintf("news_delID_%d", news.ID)
	jsonData, _ := json.Marshal(news)
	cache.Rdb.Set(cache.Ctx, key, jsonData, time.Hour)

	c.Data["json"] = map[string]interface{}{
		"code":    200,
		"message": "删除成功",
	}
	c.ServeJSON()
}
